********************************************************************************
* GENERATING YITZHAKI INDEX
* File description: Generating Yitzhaki index for all analyses 
********************************************************************************
                           

clear all
set more off


*MUNICIPALITY (main)

foreach i of numlist 2014/2017 {

use municipality dispinc_2lag LopNr year using "use/final_for_yitz" if year==`i', clear

drop if dispinc_2lag<0
drop if mi(dispinc_2lag)

drop if mi(municipality)

sort municipality

bys municipality (dispinc_2lag): gen diff=dispinc_2lag[_n+1]-dispinc_2lag[_n]
bys municipality: gen num=_N-_n
bys municipality (num): gen outcome_var=sum(diff*num)

bys municipality: gen yitz_muni=outcome_var/(_N-1)


keep LopNr year yitz_muni 

save "use/yitz/yitz_muni_`i'", replace
}


use "use/yitz/yitz_muni_2014", clear

foreach i of numlist 2015/2017 {
append using "use/yitz/yitz_muni_`i'" 

}

keep yitz_muni year LopNr

save "use/yitz/yitz_muni", replace


*MUNICIPALITY Permanent income

foreach i of numlist 2014/2017 {

use municipality dispinc_permanent LopNr year using "use/final_for_yitz" if year==`i', clear

drop if dispinc_permanent<0
drop if mi(dispinc_permanent)
drop if mi(municipality)
sort municipality

bys municipality (dispinc_permanent): gen diff=dispinc_permanent[_n+1]-dispinc_permanent[_n]
bys municipality: gen num=_N-_n
bys municipality (num): gen outcome_var=sum(diff*num)

bys municipality: gen yitz_muni_perm=outcome_var/(_N-1)

keep LopNr year yitz_muni_perm dispinc diff outcome_var

save "use/yitz/yitz_muni_perm_`i'", replace
}


use "use/yitz/yitz_muni_perm_2014", clear

foreach i of numlist 2015/2017 {
append using "use/yitz/yitz_muni_perm_`i'" 

}

save "use/yitz/yitz_muni_perm", replace


*Workplace

foreach i of numlist 2014/2017 {

use CfarNr dispinc_2lag LopNr year using "use/final_for_yitz" if year==`i', clear

drop if CfarNr==108345

drop if dispinc_2lag<0
drop if mi(dispinc_2lag)
drop if mi(CfarNr)

sort CfarNr

drop if mi(CfarNr)
bys CfarNr year: gen x=_N
drop if x<=9
drop x

bys CfarNr (dispinc_2lag): gen diff=dispinc_2lag[_n+1]-dispinc_2lag[_n]

bys CfarNr: gen num=_N-_n
bys CfarNr (num): gen outcome_var=sum(diff*num)

bys CfarNr: gen yitz_cfarnr=outcome_var/(_N-1)

bys CfarNr: egen p50_cfarnr=pctile(dispinc_2lag),p(50)

bys CfarNr: gen population_cfarnr=_N


keep LopNr year yitz_cfarnr CfarNr p50_cfarnr population_cfarnr

drop if mi(yitz_cfarnr)



save "use/yitz/yitz_cfarnr_`i'", replace
}

foreach i of numlist 2014/2016 {
append using "use/yitz/yitz_cfarnr_`i'" 

}

save "use/yitz/yitz_cfarnr", replace


*DESO
foreach i of numlist 2014/2017 {

use deso dispinc_2lag LopNr year using "use/final_for_yitz" if year==`i', clear

drop if dispinc_2lag<0
drop if mi(dispinc_2lag)
drop if mi(deso)

sort deso

bys deso (dispinc_2lag): gen diff=dispinc_2lag[_n+1]-dispinc_2lag[_n]
bys deso: gen num=_N-_n
bys deso (num): gen outcome_var=sum(diff*num)

bys deso: gen yitz_deso=outcome_var/(_N-1)

bys deso: egen p50_deso=pctile(dispinc_2lag),p(50)

bys deso: gen population_deso=_N


keep LopNr year yitz_deso deso p50_deso population_deso 

drop if mi(yitz_deso)

save "use/yitz/yitz_deso_`i'", replace
}

use "use/yitz/yitz_deso_2014", clear

foreach i of numlist 2015/2017 {
append using "use/yitz/yitz_deso_`i'" 

}

save "use/yitz/yitz_deso", replace


*AGE GROUPS
foreach i of numlist 2014/2017 {

use age municipality dispinc_2lag LopNr year using"use/final_for_yitz" if year==`i', clear

drop if dispinc_2lag<0
drop if mi(dispinc_2lag)
drop if mi(age)

gen ageid=.
replace ageid=1 if age>=20 & age<=29
replace ageid=2 if age>=30 & age<=39
replace ageid=3 if age>=40 & age<=49
replace ageid=4 if age>=50 & age<=59
replace ageid=5 if age>=60 & age<=69
replace ageid=6 if age>=70 & age<=79
replace ageid=7 if age>=80 & age<=89
replace ageid=8 if age>=90 

drop if mi(ageid)
drop if mi(municipality)

egen agegroup=group(municipality ageid)


sort agegroup

bys agegroup (dispinc_2lag): gen diff=dispinc_2lag[_n+1]-dispinc_2lag[_n]
bys agegroup: gen num=_N-_n
bys agegroup (num): gen outcome_var=sum(diff*num)

bys agegroup: gen yitz_agegroup=outcome_var/(_N-1)

bys agegroup: gen population_agegroup=_N
bys agegroup: egen p50_agegroup=pctile(dispinc_2lag),p(50)

keep LopNr year yitz_agegroup p50_agegroup population_agegroup agegroup

drop if mi(yitz_agegroup)



save "use/yitz/yitz_agegroup_`i'", replace
}

use "use/yitz/yitz_agegroup_2014", clear

foreach i of numlist 2015/2017 {
append using "use/yitz/yitz_agegroup_`i'" 

}

save "use/yitz/yitz_agegroup", replace


*MUNICIPALITY WITHOUT OUTLIERS

foreach i of numlist 2014/2017 {

use municipality dispinc_2lag LopNr year using "use/final_for_yitz" if year==`i', clear

drop if dispinc_2lag<0
drop if mi(dispinc_2lag)

bys municipality: egen p99=pctile(dispinc), p(99)
bys municipality: egen p1=pctile(dispinc), p(1)

keep if inrange(dispinc, p1, p99)

drop if mi(municipality)

sort municipality

bys municipality (dispinc_2lag): gen diff=dispinc_2lag[_n+1]-dispinc_2lag[_n]
bys municipality: gen num=_N-_n
bys municipality (num): gen outcome_var=sum(diff*num)

bys municipality: gen yitz_muni_outliers=outcome_var/(_N-1)


keep LopNr year yitz_muni_outliers 

save "use/yitz/yitz_outliers_`i'", replace
}

use "use/yitz/yitz_outliers_2014", clear

foreach i of numlist 2015/2017 {
append using "use/yitz/yitz_outliers_`i'" 

}

save "use/yitz/yitz_outliers", replace

*HUT
foreach i of numlist 2003/2009 2012 {

use age municipality dispinch LopNr year using "use/LISA/Lisa_`i'" if year==`i', clear

drop if dispinch<0
drop if mi(dispinch)
drop if mi(age)
drop if mi(municipality)


sort municipality

bys municipality (dispinch): gen diff=dispinch[_n+1]-dispinch[_n]
bys municipality: gen num=_N-_n
bys municipality (num): gen outcome_var=sum(diff*num)

bys municipality: gen yitz_muni=outcome_var/(_N-1)

bys municipality: gen population=_N


save "use/yitz/yitz_hut_`i'", replace
}


use "use/yitz/yitz_hut_2003", clear

foreach i of numlist 2004/2009 2012 {
append using "use/yitz/yitz_hut_`i'" 

}

keep yitz_muni year LopNr

duplicates drop LopNr year, force

save "use/yitz/yitz_hut", replace




